package com.zhiyou.servlet;

import com.zhiyou.dao.UserDao;
import com.zhiyou.model.Auth;
import com.zhiyou.model.User;
import com.zhiyou.service.UserService;
import com.zhiyou.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class LoginServlet extends HttpServlet {
    private UserService uS=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String uname = req.getParameter("uname");
        String upassword = req.getParameter("upassword");

        User user= uS.getUserByUnameAndUpassword(uname,upassword);
        System.out.println(user);
        if (user==null){
            req.setAttribute("err","用户名或密码错了");
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }else{
            req.getSession().setAttribute("user",user);
            List<Auth> auths= uS.findAuth(user);
            req.getSession().setAttribute("auths",auths);
            resp.sendRedirect(req.getContextPath()+"/admin.jsp");
        }
    }
}
